﻿

using Castle.Components.DictionaryAdapter;
using SBlogCore.Entitys.SysOrgManager;
using SBlogCore.Entitys.SysRoleManager;
using SBlogCore.Entitys.SystemManager;

namespace SBlogCore.Entitys.SysUserManager
{
    public class SysUserInfo : DeleteEntity, IEntityTypeConfiguration<SysUserInfo>
    {
        /// <summary>
        /// 用户名字
        /// </summary>
        [Comment("用户名字")]
        public string? UserName { get; set; }
        /// <summary>
        /// 用户年龄
        /// </summary>
        [Comment("用户年龄")]
        public int? Age { get; set; }
        /// <summary>
        /// 用户年龄
        /// </summary>
        [Comment("用户年龄")]
        public int? Sex { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        [Comment("账号")]
        public string? Account { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [Comment("密码")]
        public string? PassWord { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        [Comment("电话")]
        public string? Phone { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        [Comment("邮箱")]

        public string? Email { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        [Comment("生日")]

        public string? Birthday { get; set; }

        /// <summary>
        /// 照片
        /// </summary>
        [Comment("照片")]

        public long? PhotoId { get; set; }
        /// <summary>
        /// 文件
        /// </summary>

        public SysFileMap? SysFileMap { get; set; }

        /// <summary>
        /// 随机码
        /// </summary>
        [Comment("随机码")]

        public string? RandomCode { get; set; }

        /// <summary>
        /// 标签
        /// </summary>
        [Comment("标签")]

        public string? Tag { get; set; }

        /// <summary>
        /// 所属组织信息
        /// </summary>
        public SysOrgInfo? SysOrgInfo { get; set; }
        /// <summary>
        /// 角色  
        /// </summary>
        public List<SysOrgInfo>? SysOrgInfos { get; set; }
        /// <summary>
        /// 角色
        /// </summary>
        public List<SysRoleInfo>? SysRoles { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public List<SysUserRole> SysUserRoles { get; set; } = new EditableList<SysUserRole>();

        public  void Configure(EntityTypeBuilder<SysUserInfo> builder)
        {
            //创建一对多的主外键关系
            builder.HasOne(h => h.SysOrgInfo).WithMany(w => w.SysUserInfos).HasForeignKey(h => h.SysOrgId);
            builder.HasOne(h => h.SysFileMap).WithOne().HasForeignKey<SysUserInfo>(w => w.PhotoId);

            builder.HasMany(p => p.SysRoles)
                .WithMany(p => p.SysUsers)
                .UsingEntity<SysUserRole>(
                    u => u.HasOne(c => c.SysRole).WithMany(c => c.SysUserRoles).HasForeignKey(c => c.SysRoleId),
                    u => u.HasOne(c => c.SysUser).WithMany(c => c.SysUserRoles).HasForeignKey(c => c.SysUserId),
                    u => u.HasKey(c => new { c.SysUserId, c.SysRoleId })
                );

            builder.HasData(
                new SysUserInfo
                {
                    Id = 142307070910551, 
                    UserName = "Admin", 
                    Age = 0, 
                    Sex = 0, 
                    Account = "Admin", 
                    PassWord = "123456", 
                    Phone = "", 
                    SysOrgId = 142307070910552
                });
        }
    }
}
